package com.zy.explore.client.infrastructure.exception;


import com.zy.explore.client.interfaces.vo.ResultVO;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * description 全局异常处理类
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2022年04月17日 02:38:46
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ResultVO<?> baseExceptionHandler(BaseException e) {
        return ResultVO.getError(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO<?> exceptionHandler(Exception e) {
        e.printStackTrace();
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            return ResultVO.getError(MessageCode.BAD_REQUEST.getCode(), ex.getBindingResult().getFieldError().getDefaultMessage());
        }
        return ResultVO.getServerError(MessageCode.SYSTEM_BUSY.getMessage());
    }
}